package com.haima.sage.bigdata.api.frame.interpreter.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.haima.sage.bigdata.api.common.Constants;
import com.haima.sage.bigdata.api.frame.interpreter.ValueService;

public class FieldValueService implements ValueService {
    private SimpleDateFormat sdf = new SimpleDateFormat(Constants.DATE_PATTERN);
    private String field;

    public FieldValueService(String field) {
        this.field = field;
    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    @Override
    public String value(Map<?, ?> event) {
        Object obj = event.get(field);
        if (obj == null) {
            return null;
        }
        if (obj instanceof Date) {
            return sdf.format((Date) obj);
        }
        return obj.toString();
    }
}
